/*   
 * Copyright 2009-2010 Rob Schoening
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package org.beaconframework.sampler;

import java.awt.print.Book;

import javax.xml.parsers.DocumentBuilderFactory;

import org.beaconframework.util.xml.DomUtil;
import org.beaconframework.util.xml.SpringDomSource;
import org.beaconframework.vaadin.data.XmlContainerBuilder;
import org.beaconframework.vaadin.data.XmlItemContainer;
import org.beaconframework.vaadin.ui.ApplicationLayoutWindow;
import org.beaconframework.vaadin.ui.GenericApplication;
import org.beaconframework.vaadin.ui.SimpleTree;
import org.beaconframework.vaadin.util.VaadinContext;
import org.w3c.dom.Document;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;

public class SamplerApplication extends GenericApplication {

	/**
     * 
     */
    private static final long serialVersionUID = 8187008425505214041L;

    @Override
	public synchronized void init() {
		// TODO Auto-generated method stub
		super.init();

		try {
		ApplicationLayoutWindow w = new ApplicationLayoutWindow();

		setMainWindow(w);

		Table t = new Table("Test");
	
		XmlItemContainer xic = new XmlItemContainer();
		
		System.out.println();
		
		
	
		
		XmlContainerBuilder cb = new XmlContainerBuilder();
	
		cb.setXmlSource(new SpringDomSource(VaadinContext.getApplicationContext(),"classpath:books.xml"));
		
		//cb.setXmlSource(document);
		//cb.setRowXPath("/result_set/results/book");
		cb.setRowXPath(".//book");
		cb.addXPathProperty("test", "book_details/book_detail/title/text()");
		cb.addXPathProperty("Author", ".//author/text()");
		cb.addXPathProperty("Publisher", ".//publisher/text()");
		
		IndexedContainer ic = cb.createIndexedContainer();
		
		//cb.updateIndexedContainer(document, ic);
		t.setContainerDataSource(ic);
		/*
		IndexedContainer ic = new IndexedContainer();
		ic.addContainerProperty("test", String.class, "default");
		
		Object id = ic.addItem();
		Item item = ic.getItem(id);
		item.getItemProperty("test").setValue("helo!");
		
		t.setContainerDataSource(ic);
		
		
		GroovySampler gs = new GroovySampler();
		gs.setScriptResource("scripts/label.groovy");
	//gs.setScript("return new com.vaadin.ui.Label(\"test\");");
		gs.update();
		*/
		
		w.setMainComponent(t);

		w.setCaption("Beacon Demo Sampler");

		SimpleTree tc = new SimpleTree();
		tc.setImmediate(true);
		

	/*
		for (Object id : tc.rootItemIds()) {
			tc.expandItemsRecursively(id);
		}
*/
		w.setLeftComponent(tc);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
	}

}
